package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Override
    public void add(SysDictData sysDictData) {
        sysDictDataMapper.insert(sysDictData);
    }

    @Override
    public void deleteBatch(SysDictData sysDictData) {
        sysDictDataMapper.deleteByPrimaryKey(sysDictData);
    }

    @Override
    public void edit(SysDictData sysDictData) {
        sysDictDataMapper.updateByPrimaryKeySelective(sysDictData);
    }
    

    @Override
    public List<SysDictData> getDictTypeList(String uid) {
        return sysDictDataMapper.getDictTypeListByUid(uid);

    }

    @Override
    public Page<SysDictData> getList(Map<String, Object> searchMap) {
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = createExample(searchMap);
        return (Page<SysDictData>) sysDictDataMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(SysDictData.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
                criteria.andLike("dictValue", "%" + searchMap.get("keyword") + "%");
            }
            if (searchMap.get("cssClass") != null && !"".equals(searchMap.get("cssClass"))) {
                criteria.andEqualTo("cssClass", searchMap.get("cssClass"));
            }
            if (searchMap.get("dictLabel") != null && !"".equals(searchMap.get("dictLabel"))) {
                criteria.andEqualTo("dictLabel", searchMap.get("dictLabel"));
            }
            if (searchMap.get("dictTypeUid") != null && !"".equals(searchMap.get("dictTypeUid"))) {
                criteria.andEqualTo("dictTypeUid", searchMap.get("dictTypeUid"));
            }
            if (searchMap.get("dictValue") != null && !"".equals(searchMap.get("dictValue"))) {
                criteria.andEqualTo("dictValue", searchMap.get("dictValue"));
            }
            if (searchMap.get("isPublish") != null && !"".equals(searchMap.get("isPublish"))) {
                criteria.andEqualTo("isPublish", searchMap.get("isPublish"));
            }
            if (searchMap.get("listClass") != null && !"".equals(searchMap.get("listClass"))) {
                criteria.andEqualTo("listClass", searchMap.get("listClass"));
            }
            if (searchMap.get("isDefault") != null && !"".equals(searchMap.get("isDefault"))) {
                criteria.andEqualTo("isDefault", searchMap.get("isDefault"));
            }
            if (searchMap.get("oid") != null && !"".equals(searchMap.get("oid"))) {
                criteria.andEqualTo("oid", searchMap.get("oid"));
            }
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andEqualTo("uid", searchMap.get("uid"));
            }
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("remark") != null && !"".equals(searchMap.get("remark"))) {
                criteria.andEqualTo("remark", searchMap.get("remark"));
            }
            if (searchMap.get("sort") != null && !"".equals(searchMap.get("sort"))) {
                criteria.andEqualTo("sort", searchMap.get("sort"));
            }
        }
        return example;
    }
}
